package com.example.gamecenter.activitys;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Toolbar;

import androidx.appcompat.app.AppCompatActivity;

import com.example.gamecenter.R;
import com.example.gamecenter.utils.X5WebView;
import com.tencent.smtt.sdk.QbSdk;

public class BaseTencentWebActivity extends AppCompatActivity {

    X5WebView myX5WebView;
    // String baseurl = "https://h5itokenla.147358.top/index";
    String baseurl = "https://test-pay-h5.axzo.cn/#/pages/payroll/salaryApply/salaryManagement";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_tencen_web);
        
        //x5内核初始化接口
        initQbsdk();
        //如果用到布局文件的ToolBar才需要设置（非必须）
//        Toolbar toolbar = findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        getSupportActionBar().setHomeButtonEnabled(true);
//        getSupportActionBar().setTitle("实现文档");
        
        myX5WebView = findViewById(R.id.myX5WebView);
        //清理缓存和历史（非必须）
        myX5WebView.clearCache(true);
        myX5WebView.clearHistory();
        //视频为了避免闪屏和透明问题，需要如下设置
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
		//启用硬件加速
        initHardwareAccelerate();
		//加载url
        myX5WebView.loadUrl(baseurl);

    }
	//x5内核初始化接口
    private void initQbsdk() {
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg0) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.d("app", "腾讯X5内核初始化回调 onViewInitFinished is " + arg0);
            }
            @Override
            public void onCoreInitFinished() {
                Log.d("app", "腾讯X5内核初始化回调 onCoreInitFinished");
            }
        });
    }

    //启用硬件加速：由于硬件加速自身并非完美无缺，所以Android提供选项来打开或者关闭硬件加速，默认是关闭。可以在4个级别上打开或者关闭硬件加速
    private void initHardwareAccelerate() {
        try {
            if (Build.VERSION.SDK_INT >= 11) {
            	//Window级别硬件加速
                getWindow()
                        .setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }
    }

    //设置返回键监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (myX5WebView != null && myX5WebView.canGoBack()) {
                myX5WebView.goBack();
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    //释放资源，防止内存泄露
    @Override
    protected void onDestroy() {
        if (myX5WebView != null) {
            myX5WebView.destroy();
        }
        super.onDestroy();
    }


}

